package cn.yunhe.service;

import cn.yunhe.dao.FboardDao;
import cn.yunhe.entity.Fboard;
import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class FboardService {

    SqlSession session=null;
    FboardDao fboardDao=null;

    /**
     * 初始化&&反射
     */
    public void initial(){
        session=SqlSessionFactoryUtil.getSqlSession();
        fboardDao=session.getMapper(FboardDao.class);
    }

    /**
     * 提交&&销毁
     */
    public void destory(){
        session.commit();
        if (session!=null){
            session.close();
        }
    }

    /**
     * 根据一级版块fbid获得对应 一级版块完整信息
     * @param fbid
     * @return
     */
    public Fboard searchFboardByFbId(int fbid){
        initial();
        Fboard fboard=fboardDao.searchFboardByFbId(fbid);
        destory();
        return fboard;
    }

    /**
     * 根据一级版块名称fbname获取到当前 一级版块的信息
     * @param fbname
     * @return
     */
    public Fboard searchFboardByFbname(String fbname){
        initial();
        Fboard fboard=fboardDao.searchFboardByFbname(fbname);
        destory();
        return fboard;
    }

    /**
     * 新增一级版块,实际参数fbname
     * @param fbname
     * @return
     */
    public boolean addFboard( String fbname){
        initial();
        //s1
        Fboard fboard=new Fboard();
        fboard.setFbname(fbname);
        //s2
        boolean flag=fboardDao.addFboard(fboard);
        destory();
        return flag;
    }

    /**
     * 更新某一个一级版块的名称
     * @param fbid
     * @param fbname
     * @return
     */
    public boolean updateFboard(int fbid,String fbname){
        initial();
        //s1
        Fboard fboard=new Fboard();
        fboard.setFbid(fbid);
        fboard.setFbname(fbname);
        //s2
        boolean flag=fboardDao.updateFboard(fboard);
        destory();
        return flag;
    }

    /**
     * 删除某一个一级版块,实际参数fbid,
     * 删除功能的实质是更改当前状态fbflag:0-显示,1-隐藏
     * @param fbid
     * @return
     */
    public boolean deleteFboard(int fbid){
        initial();
        int fbflag=-1;
        //s1定位到对应的一级版块的完整信息
        Fboard fboard=fboardDao.searchFboardByFbId(fbid);
        //s2
        if (fboard.getFbflag()==0){
            fbflag=1;
        }else if (fboard.getFbflag()==1){
            fbflag=0;
        }
        fboard.setFbflag(fbflag);
        //s3
        boolean flag=fboardDao.deleteFboard(fboard);
        destory();
        return flag;
    }


   /* public List<Fboard> findSboardByFbid(){
        initial();
        List<Fboard>fboardList=fboardDao.allFboard();
        destory();
        return  fboardList;

    }*/

    public List<Fboard> fboardListByFbflag(int fbflag){
        initial();
        //s1
        Fboard fboard=new Fboard();
        fboard.setFbflag(fbflag);
        //s2
         List<Fboard> fboardList=fboardDao.fboardListByFbflag(fboard);
         destory();
         return  fboardList;
    }

    /**
     * 查询所有一级菜单
     * @return
     */
    public List<Fboard> getAllFBoard(){
        initial();
        List<Fboard> fboardList=fboardDao.allFboard();
        destory();
        return  fboardList;
    }
}
